package com.ruoyi.job.service;

import com.ruoyi.system.api.RemoteArchiveTaskService;
import com.ruoyi.system.api.RemoteUserService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * XxlJob 多服务调用
 *
 * @author Lion Li
 */
@Slf4j
@Service
public class ArchiveJobService {

    @DubboReference
    private RemoteArchiveTaskService archiveTaskService;

    /**
     * 多服务调用
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    @XxlJob("archiveJobHandler")
    public void archiveJobHandler() throws Exception {
        String param = XxlJobHelper.getJobParam();
        XxlJobHelper.log("XXL-JOB, archiveJobHandler start: {}", param);
        log.info("XXL-JOB, archiveJobHandler start: {}", param);

        if (!StringUtils.isNumeric(param)) {
            XxlJobHelper.log("XXL-JOB, archiveJobHandler end: param is not number");
            log.info("XXL-JOB, archiveJobHandler end: param is not number");
            return;
        }

        Long taskId = Long.valueOf(param);
        archiveTaskService.execute(taskId);
    }
}
